<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Oauth_redirect extends CI_Controller {

	function __construct() {
		parent::__construct();
		$this->load->library('cuiqg');
	}

	public function baidu() {

		$state = $this->input->get('state', true);
		
		if($state && $state == 'cuiqg_ac') {

			$code = $this->input->get('code', true);
			$result = $this->cuiqg->getAccessToken($code);

			$tmpArr = json_decode($result, true);
			if(!isset($tmpArr['error'])) {
				$this->cuiqg->doCache('access_token', $tmpArr['access_token'], $tmpArr['expires_in']);
				$this->cuiqg->doCache('refresh_token', $tmpArr['refresh_token'], 31536000);
				redirect('home', 'refresh');
			} else {
				exit($tmpArr['error_description']);
			}
		}
	}
}